<?php
set_time_limit(0);
$returninfo = null;
  function fetch($url,$ispost=false,$header = array(),$cookiestr = '',$referer = '',$poxyip = array()){
  $ch = curl_init(); 
  $options = array(
          CURLOPT_URL => $url,
          CURLOPT_HEADER => false, 
          CURLOPT_POST => $ispost, 
          CURLOPT_RETURNTRANSFER => true, 
          CURLOPT_FOLLOWLOCATION => 1,
          CURLOPT_SSL_VERIFYPEER => false, 
          CURLOPT_SSL_VERIFYHOST => false,
          CURLOPT_REFERER => $referer,
          CURLOPT_CONNECTTIMEOUT=> 30,
          CURLOPT_TIMEOUT=>45
      );
     
    if($ispost){
    	$options[CURLOPT_POSTFIELDS] = $ispost;
    }
    if(!empty($header)){
    	$options[CURLOPT_HTTPHEADER] = $header;
    }
    
    if(!empty($cookiestr)){
     $options[CURLOPT_COOKIE] = $cookiestr;
    }
    
    if(!empty($poxyip)){
      $options[CURLOPT_PROXYAUTH] = CURLAUTH_BASIC;      
      $options[CURLOPT_PROXYTYPE] = CURLPROXY_HTTP;
      $options[CURLOPT_PROXY] = $poxyip[0];      
      $options[CURLOPT_PROXYPORT] = $poxyip[1];
    }
    
    curl_setopt_array($ch, $options); 
    $ispost = curl_exec($ch);
    $GLOBALS['returninfo'] = curl_getinfo($ch);
    //var_dump($ispost);
    curl_close($ch);
    return $ispost;
  }


$logininfo = [];
$logininfo['login_password'] = '登录密码'; 
$logininfo['domain'] = 'foglede.gitee.com';
$logininfo['login_email'] = '伊妹儿';

$myip = fetch('http://ddns.oray.com/checkip');
$myip = simplexml_load_string($myip);
$myip = json_encode($myip);
$myip = json_decode($myip,true);
if(isset($myip['body'])){
  $myip = str_replace('Current IP Address: ','',$myip['body']);
  //var_dump($myip);
}
   //获取域名信息 修改域名
 $domaininfo = fetch('https://dnsapi.cn/Domain.Info',"domain={$logininfo['domain']}&login_email={$logininfo['login_email']}&login_password={$logininfo['login_password']}&format=json") ;
 $domaininfo = json_decode($domaininfo,true);
 $domaininfoid = 0;
 if(isset($domaininfo['domain']['id'])){
  $domaininfoid = $domaininfo['domain']['id'];
 }
 //var_dump($domaininfoid);
 //获取域名信息完
 
 //获取记录
 $domaininfo = fetch('https://dnsapi.cn/Record.List',"domain_id={$domaininfoid}&length=100&login_email={$logininfo['login_email']}&login_password={$logininfo['login_password']}&format=json") ;
 $domaininfo = json_decode($domaininfo,true);
 $hnresult = 0;
 if(isset($domaininfo['records'])){
  $dmrecords = $domaininfo['records'];
  foreach($dmrecords as $ditem){
    if(strcasecmp($ditem['name'],'hn') === 0){
      if(strcasecmp($ditem['value'],$myip) === 0){
       echo 'this ip is same!';//相同IP就不要改了
       echo PHP_EOL;
       break;
      }
      $hnresult = $ditem['id'];
       //修改记录 
      $domaininfo = fetch('https://dnsapi.cn/Record.Modify',"domain_id={$domaininfoid}&record_id={$hnresult}&record_type=A&record_line=默认&value=".urlencode($myip)."&mx=1&login_email={$logininfo['login_email']}&login_password={$logininfo['login_password']}&format=json") ;
      $domaininfo = json_decode($domaininfo,true);
      break;
    }
  }
 }
 
 //var_dump($hnresult);